package pers.lws.erp.controller;


import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.lws.erp.common.result.ResultBody;
import pers.lws.erp.entity.SysRole;
import pers.lws.erp.entity.vo.LoginUserVO;
import pers.lws.erp.entity.vo.RoleVO;
import pers.lws.erp.service.ISysRoleService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuws
 * @since 2020-09-11
 */
@RestController
@RequestMapping("/sysRole")
@Slf4j
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @GetMapping("/findRoles")
    public ResultBody findRoles(HttpServletRequest request) {
        //从请求头获取token
        String authorization = request.getHeader("Authorization");
        //json转换成java对象
        LoginUserVO loginUserVo = JSONUtil.toBean(authorization, LoginUserVO.class);

        List<SysRole> rolesByUserId = sysRoleService.findRolesByUserId(loginUserVo.getUserId());
        //返回前端的角色对象
        RoleVO roleVO = null;
        if (rolesByUserId.size() > 0) {
            roleVO = new RoleVO();
            roleVO.setRole(rolesByUserId.get(0).getRoleName());
            roleVO.setPermissions(rolesByUserId.get(0).getRoleDesc());
            log.info("返回rolevo对象: " + roleVO.toString());
            return ResultBody.success("获取权限", roleVO);
        } else {
            return ResultBody.error("无权限");
        }
    }

}
